<template>
<!-- 学业中断 -->
    <el-card class="box-card">
        <div slot="header" class="card-heaer">
            <span style="line-height: 36px;">学业中断理赔信息录入</span>
            <el-button type="primary" style="float:right" @click="up" v-if="state!='查看'&&state!='结果'">提交</el-button>
        </div>

        <el-form ref="form" :model="obj" :rules.sync="modelrules" label-width="180px" >

            <el-form-item label="开学日期:" prop="BILL_ACADEMIC_STARTTIME">
                <el-date-picker v-model="obj.BILL_ACADEMIC_STARTTIME" type="date" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="学业中断日期:" prop="BILL_ACADEMIC_ENDTIME">
                <el-date-picker v-model="obj.BILL_ACADEMIC_ENDTIME" type="date" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="原学期终止时间:" prop="BILL_ACADEMIC_PLANTIME">
                <el-date-picker v-model="obj.BILL_ACADEMIC_PLANTIME" type="date" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="学业中断原因:" prop="BILL_ACADEMIC_REASON">
                <el-input v-model="obj.BILL_ACADEMIC_REASON" :disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="详情描述:" prop="BILL_ACADEMIC_DETAILS">
                <el-input type="textarea" v-model="obj.BILL_ACADEMIC_DETAILS" :disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="实际缴纳学费:" prop="BILL_ACADEMIC_MONEY">
                <el-input v-model="obj.BILL_ACADEMIC_MONEY" :disabled="disabled" style="float:left; width:200px; padding-left:5px"></el-input>
            </el-form-item>
            <el-form-item label="学校已退还的学费:" prop="BILL_ACADEMIC_BACKMONEY">
                <el-input v-model="obj.BILL_ACADEMIC_BACKMONEY" :disabled="disabled" style="float:left; width:200px; padding-left:5px"></el-input>
            </el-form-item>

            <el-form-item label="附件上传:" v-if="state!='理赔审核' && state!='查看' && state!='结果'">
                <el-button type="primary" class="fileselect">
                    <input type="file" @change="upload" value="" id="fileup"> 上传附件
                </el-button>
            </el-form-item>
            <el-form-item label="附件下载:" v-if="state=='理赔审核'||state=='结果' ||state=='查看'">
                <el-button type="primary" class="fileselect" @click="download">
                    下载附件
                </el-button>
            </el-form-item>

            <el-form-item label="审核:" v-if="state=='理赔审核'">
                <el-select placeholder="审核" v-model="BILL_DIED_STATE">
                    <el-option label="审核通过" value="需计算"></el-option>
                    <el-option label="退回重新填写" value="需理赔修改"></el-option>
                    <el-option label="拒赔" value="理赔审核不通过"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="不通过原因:" v-if="BILL_DIED_STATE=='理赔审核不通过'||BILL_DIED_STATE=='需理赔修改'|| state=='查看'||state=='修改'">
                <el-input type="textarea" v-model="BILL_DIED_WHY" :disabled="state=='查看'||state=='修改'"></el-input>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
import {
    mapState
} from 'vuex';
import getid from '@/js/getid';
export default {
    data() {
        return {

            showwhy: false,
            hospatialList: [],
            issuccess: false, //是否通过,
            disabled: false,
            obj: this.$store.state.lipeiDuty.studyBreak,
            ischeck: false,
            BILL_DIED_STATE: "",
            BILL_DIED_WHY: "",
            file: "",//文件地址
    

        }
    },
    props: {
        state: {
            type: String,
            default: "新增"
        }
    },
    computed: mapState({
        // 报案人信息
        modelrules: state => state.lipeiDuty.studyBreakRules,
        address: state => state.select.address,
    }),
    methods: {
        up() {
            if (this.state == "新增") {
                this.add();
            } else {
                this.changer();
            }

        },
        changer() {
            var model = JSON.parse(JSON.stringify(this.obj));
            if (this.state == "修改") {
                model.BILL_ACADEMIC_STATE = "需理赔审核";
                  model.BILL_ACADEMIC_WHY = "";
            } else {
                model.BILL_ACADEMIC_STATE = this.BILL_DIED_STATE;
            }
            model.RI_ACADEMIC_ID = this.$route.query["id"];
            model.BILL_ACADEMIC_WHY = this.BILL_DIED_WHY;
            model.BILL_ACADEMIC_DOC = this.file;
            this.$http.put("BILL_ACADEMIC", model).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    this.$notify({
                        title: '审核成功',
                        message: '审核成功',
                        type: 'success'
                    });
                    this.$router.go(-1);
                } else {
                    this.$notify.error({
                        title: '提交失败',
                        message: data.msg
                    });
                }
            }, response => {
                this.loading = false;
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });
        },
        add() {
            if (this.file == "") {
                this.$notify.error({
                    title: '数据不完整',
                    message: '请上传附件'
                });
                return;
            }
            var model = JSON.parse(JSON.stringify(this.obj));
            model.BILL_ACADEMIC_ID = getid.getid();

            model.BILL_ACADEMIC_STATE = "理赔审核";
            model.RI_ACADEMIC_ID = this.$route.query["id"];
            model.BILL_ACADEMIC_DOC = this.file;

            this.$http.post("BILL_ACADEMIC", model).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    this.$notify({
                        title: '添加成功',
                        message: '添加成功',
                        type: 'success'
                    });
                    this.$router.go(-1);
                } else {
                    this.$notify.error({
                        title: '提交失败',
                        message: data.msg
                    });
                }
            }, response => {
                this.loading = false;
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });
        },
        download() {
            var url = this.$http.options.root;
            var base = url.substring(0, url.length - 3);
            window.open(base + "Upload/" + this.file);
        },
        upload(event) {
            let file = event.target.files[0];
            // if (file.type != "application/x-zip-compressed") {
            //     this.$notify({
            //         title: '文件格式不正确',
            //         message: '仅支持zip格式文件',
            //         type: 'warning'
            //     });
            //     return;
            // }
            this.loading = true;
            if (file) {
                const formData = new FormData()
                formData.append('file', file)
                this.$http.post('ReportInfomation/file', formData, {
                    progress(event) {
                        //传递给父组件的progress方法

                        // self.$emit('progress', parseFloat(event.loaded / event.total * 100), flag)
                    }
                }).then(response => {
                    var data = response.body;
                    this.loading = false;
                    if (data.code == 1) {

                        this.file = data.msg[0];
                        this.$notify({
                            title: '附件上传成功',
                            message: '附件上传成功',
                            type: 'success'
                        });
                        document.getElementById("fileup").value = "";

                    } else {
                        this.loding = false;
                        this.$notify.error({
                            title: '提交失败',
                            message: data.msg
                        });
                        document.getElementById("fileup").value = "";
                    }
                }, response => {
                    this.loading = false;
                    this.$notify.error({
                        title: '网络错误',
                        message: '网络错误'
                    });
                    this.loding = false;
                    document.getElementById("fileup").value = "";
                })


            }
        }
    },

    activated() {             
        this.obj = this.$store.state.lipeiDuty.studyBreak;
            
        this.obj.BILL_ACADEMIC_ID= "",
         this.obj.BILL_ACADEMIC_STARTTIME= "",
         this.obj.BILL_ACADEMIC_ENDTIME= "",
         this.obj.BILL_ACADEMIC_PLANTIME= "",
         this.obj.BILL_ACADEMIC_REASON= "",
         this.obj.BILL_ACADEMIC_DETAILS= "",
         this.obj.BILL_ACADEMIC_MONEY= "",
         this.obj.BILL_ACADEMIC_BACKMONEY= "",
         this.obj.RI_ACADEMIC_ID= "",
         this.obj.BILL_ACADEMIC_STATE= "",
         this.obj.BILL_ACADEMIC_WHY= "";
   
        this.file = "";
        if (this.state != "新增") {
            this.BILL_DIED_STATE = "";
            this.BILL_DIED_WHY = "";
            var url = "BILL_ACADEMIC/" + this.$route.query["id"];

            this.$http.get(url).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    this.obj = data.result[0];
                    this.BILL_DIED_WHY = data.result[0].BILL_ACADEMIC_WHY;
                    this.obj.BILL_ACADEMIC_MONEY = data.result[0].BILL_ACADEMIC_MONEY + '';
                    this.obj.BILL_ACADEMIC_BACKMONEY = data.result[0].BILL_ACADEMIC_BACKMONEY + '';

                    this.file = data.result[0].BILL_ACADEMIC_DOC;


                } else {

                }
            }, response => {
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });
        }
    }


}
</script>
